package com.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dao.PartyOrgBranchDao;
import com.entity.Td_punished;
import com.entity.Td_tblPartyOrgBranch;
import com.entity.Td_tblpartyorgmembers;
import com.service.inte.PunishedServiceInte;
import com.service.inte.Td_tblPartyRelationshipTransferServiceInte;
import com.util.TreeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.service.inte.MemberServiceInte;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MemberController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MemberServiceInte memberServiceInte;

    @Autowired
    private PartyOrgBranchDao partyOrgBranchDao;

    @Autowired
    private PunishedServiceInte punishedServiceInte;

    @Autowired
    private Td_tblPartyRelationshipTransferServiceInte partyRelationshipTransferServiceInte;

    @GetMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response) {
        return "index";
    }

    @GetMapping("getBranchMembersForTree")
    @ResponseBody
    public String getBranchMembersForTree() {
        List<Td_tblPartyOrgBranch> tblPartyOrgBranchall = partyOrgBranchDao.getAllBrnch();
        List<Td_tblpartyorgmembers> memberBranchModels = memberServiceInte.getAllMember();

        List<TreeBuilder.Node> allNodes = new ArrayList<TreeBuilder.Node>();
        for (int i = 0; i < tblPartyOrgBranchall.size(); i++) {
            TreeBuilder.Node tb = new TreeBuilder.Node();
            tb.setFd_BranchID(tblPartyOrgBranchall.get(i).getFd_BranchID());
            tb.setText(tblPartyOrgBranchall.get(i).getFd_BranchName());
            tb.setFd_ParentBranchID(String.valueOf(tblPartyOrgBranchall.get(i).getFd_ParentBranchID()));
            tb.setFd_leveltype(Integer.valueOf(tblPartyOrgBranchall.get(i).getFd_leveltype()));
            allNodes.add(tb);
        }
        for (Td_tblpartyorgmembers memberBranchModel : memberBranchModels) {
            TreeBuilder.Node tb = new TreeBuilder.Node();
            tb.setFd_LoginID(memberBranchModel.getFd_loginId());
            tb.setText(memberBranchModel.getFd_UserName());
            tb.setFd_ParentBranchID(String.valueOf(memberBranchModel.getFd_branchId()));
            tb.setFd_leveltype(0);
            allNodes.add(tb);
        }
        TreeBuilder tbs = new TreeBuilder(allNodes);
        String returns = tbs.buildJSONTree();
        System.out.println(tbs);
        return JSONArray.toJSONString(returns);
    }

    @GetMapping("turnPersonInformation")
    public String turnPersonInformation(int loginId, HttpServletRequest request) {
        Td_tblpartyorgmembers member = memberServiceInte.getByLoginId(loginId);
        request.setAttribute("member", member);
        StringBuffer submitValue = new StringBuffer();
        switch (member.getFd_menbertype()) {
            case 1:
                submitValue.append("申请成为积极分子");
                break;
            case 2:
                submitValue.append("申请成为发展对象");
                break;
            case 3:
                submitValue.append("申请成为预备党员");
                break;
            case 4:
                submitValue.append("申请成为正式党员");
                break;
        }
        request.setAttribute("submitValue", submitValue);
        return "member/personInfomation.html";
    }

    @GetMapping("memberManage")
    public String memberManage() {
        return "member/memberPage.html";
    }

    @PostMapping("memberSearch")
    @ResponseBody
    public List<Object[]> memberSearch(String name, int page) {
        return memberServiceInte.getMemberByName(name, page);
    }

    @GetMapping("turnToMemberSearch")
    public String turnToMemberSearch() {
        return "member/memberSearch.html";
    }

    @GetMapping("memberDetail")
    public String memberDetail(int loginId, HttpServletRequest request, Model model) {
        model.addAttribute("detail", memberServiceInte.getByLoginId(loginId));
        model.addAttribute("punishCount",punishedServiceInte.memberPunishCount(loginId));
        model.addAttribute("transferCount",partyRelationshipTransferServiceInte.memberTransferCount(loginId));
        return "member/detail.html";
    }

    @GetMapping("turnPunished")
    public String turnPunished(int loginId, HttpServletRequest request) {
        request.setAttribute("member", memberServiceInte.getByLoginId(loginId));
        request.setAttribute("disposer", memberServiceInte.getByLoginId(1868));
        return "member/punish/punishment.html";
    }

    @PostMapping("savePunished")
    public String savePunished(Td_punished td_punished) {
        td_punished.setFd_punishedTime(new Date(System.currentTimeMillis()));
        if (punishedServiceInte.save(td_punished) > 0) {
            return "member/memberPage.html";
        } else {
            return null;
        }
    }

    @GetMapping("turnNativeManage")
    public String turnMemberManage() {
        return "member/transfer/transferManage.html";
    }

    @GetMapping("turnNativeDetail")
    public String turnNativeDetail(HttpServletRequest request, int loginId) {
        request.setAttribute("detail", memberServiceInte.getByLoginId(loginId));
        return "member/nativeDetail";
    }

    @GetMapping("turnToPunishSearch")
    public String turnToPunishSearch() {
        return "member/punish/memberSearch.html";
    }

    @GetMapping("turnPunishRecord")
    public String turnPunishRecord() {
        return "member/punish/punishRecord.html";
    }

    @PostMapping("getPunishRecord")
    @ResponseBody
    public List<Object[]> getRunishRecord(String name, int page) {
        return punishedServiceInte.getByName(name, page);
    }

    @PostMapping("getApprocess")
    @ResponseBody
    public List<Object[]> getApprocess(String name, int type, int page) {
        return memberServiceInte.getAllApprroval(type, name, page);
    }

    @GetMapping("turnApprocess")
    public String turnApprocess(HttpServletRequest request) {
        request.setAttribute("member",memberServiceInte.getByLoginId(1866));
        return "member/personInfomation.html";
    }

    @GetMapping("approcess")
    public String approcess(int loginId, HttpServletRequest request) {
        request.setAttribute("member", memberServiceInte.getByLoginId(loginId));
        return "member/process/personInfomation.html";
    }

    @PostMapping("addMember")
    public String updateApprocess(Td_tblpartyorgmembers td_tblpartyorgmembers) {
        int approcessLoginId = 1869;
        String approcessName = "张大龙";
        Date now = new Date(System.currentTimeMillis());
        if (StringUtils.isEmpty(td_tblpartyorgmembers.getFd_reason())) {
            td_tblpartyorgmembers.setFd_menbertype(td_tblpartyorgmembers.getFd_menbertype() + 1);
            td_tblpartyorgmembers.setFd_istype(0);
        } else {
            td_tblpartyorgmembers.setFd_istype(2);
        }
        System.out.println("类型：" + td_tblpartyorgmembers.getFd_menbertype());
        switch (td_tblpartyorgmembers.getFd_menbertype()) {
            case 2:
                td_tblpartyorgmembers.setFd_activistsperson(approcessName);
                td_tblpartyorgmembers.setFd_activistsdate(now);
                break;
            case 3:
                td_tblpartyorgmembers.setFd_devtargetperson(approcessName);
                td_tblpartyorgmembers.setFd_devtargetdate(now);
                break;
            case 4:
                td_tblpartyorgmembers.setFd_probationaryperson(approcessName);
                td_tblpartyorgmembers.setFd_probationarydate(now);
                break;
            case 5:
                td_tblpartyorgmembers.setFd_partymenberperson(approcessName);
                td_tblpartyorgmembers.setFd_BeRegularDate(now);
                td_tblpartyorgmembers.setFd_passpwrd("gdj123");
                td_tblpartyorgmembers.setFd_PoliticsLineament("中国共产党党员");
                break;
        }
        System.out.println(td_tblpartyorgmembers.toString());
        memberServiceInte.saveOrUpdate(td_tblpartyorgmembers);
        return "member/memberPage.html";
    }

    @PostMapping("saveMember")
    public String saveMember(Td_tblpartyorgmembers td_tblpartyorgmembers, @RequestParam("imgName") MultipartFile imgName) throws IOException {
        td_tblpartyorgmembers.setFd_istype(2);
        if (imgName != null) {
            td_tblpartyorgmembers.setFd_ImageUrl(memberServiceInte.uploadMemberImage(imgName, td_tblpartyorgmembers.getFd_loginId()));
        }
        if (td_tblpartyorgmembers.getFd_menbertype()==1){
            td_tblpartyorgmembers.setFd_JoinPartyDate(new Date(System.currentTimeMillis()));
        }
        memberServiceInte.saveOrUpdate(td_tblpartyorgmembers);
        return "member/memberPage.html";
    }

    @GetMapping("memberTransfer")
    public String memberTransfer(int loginId,HttpServletRequest request){
        request.setAttribute("loginId",loginId);
        return "member/transfer/memberTransfer.html";
    }

    @GetMapping("memberPunish")
    public String memberPunish(int loginId,HttpServletRequest request){
        request.setAttribute("loginId",loginId);
        return "member/punish/memberPunish.html";
    }

    @PostMapping("transferLog")
    @ResponseBody
    public List<Object[]> transferLog(int loginId,int page){
        return partyRelationshipTransferServiceInte.getAllFinishByLoginId(loginId,page);
    }

    @PostMapping("punishLog")
    @ResponseBody
    public List<Object[]> punishLog( int loginId, int page){
        return punishedServiceInte.getByloginId(loginId,page);
    }
}
